package com.windhc;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import com.windhc.model.Cron;
import com.windhc.service.CronService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hc
 */
@SpringBootApplication
public class Application implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(CronService.class);

    @Resource
    private CronService cronService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("init");
        // 初始化数据库中的任务
        List<Cron> list = cronService.findAll();
        for (Cron cron : list) {
            CronUtil.schedule(cron.getSchedulerId(), cron.getCronStr(), new Task() {
                @Override
                public void execute() {
                    logger.info(".....");
                }
            });
        }
        CronUtil.setMatchSecond(true);
        CronUtil.start();
    }
}
